﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class RepairRequestListCtrl : BaseListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        const string REPAIR_JOB_PROBLEMS = "REPAIR_JOB_PROBLEMS";
        public RepairJobProblems JobProblems
        {
            get
            {
                return (RepairJobProblems)Session[REPAIR_JOB_PROBLEMS];
            }
            set
            {
                Session[REPAIR_JOB_PROBLEMS] = value;
            }
        }

        public RepairJob Jobs 
        {
            get
            {
                return (RepairJob)SourceObject;
            }
            set
            {
                SourceObject = value;
            }
        }

        public bool AllowUpdating
        {
            get { return ViewState[ClientID + "_AllowUpdate"] != null ? (bool)ViewState[ClientID + "_AllowUpdate"] : true; }
            set { ViewState[ClientID + "_AllowUpdate"] = value; }
        }

        public String GetProblems(Object jobID)
        {
            if (JobProblems == null)
                return "";

            String res = "";
            String oldFilter = JobProblems.Filter;
            JobProblems.Filter = RepairJobProblems.ColumnNames.RepairJobID + "=" + jobID.ToString();

            if (JobProblems.RowCount > 0)
            {
                JobProblems.Rewind();
                for (int i = 0; i < JobProblems.RowCount; i++)
                {
                    string str = "";
                    str = String.Format("<div>{0}&nbsp;&nbsp;{1:C}</div>", JobProblems.Description, JobProblems.Price);
                    res += str;

                    JobProblems.MoveNext();
                }
            }

            JobProblems.Filter = oldFilter;

            return res;
        }

        LoanPhones loanPhones;
        public LoanPhones LoanPhones
        {
            get
            {
                if (loanPhones == null)
                    loanPhones = LoanPhones.LoadActiveLoanPhone();
                return loanPhones;
            }
            set
            {
                loanPhones = value;
            }
        }

        public string GetLoanPhone(int id)
        {
            string oldFilter = LoanPhones.Filter;
            LoanPhones.Filter = LoanPhones.ColumnNames.ID + "=" + id.ToString();
            string res = "";
            if (LoanPhones.RowCount > 0)
            {
                res = "<br/><span class='labelHint'><b>Loan phone:</b> " + LoanPhones.s_ProductName + "[" + LoanPhones.s_IMEI + "]</span>";
            }

            LoanPhones.Filter = oldFilter;
            return res;
        }

        public int[] GetCheckedItemIDs()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

            String[] ids = checkedItemID.Split(new char[] { ',' });
            int[] items = new int[ids.Length];
            for (int i = 0; i < ids.Length; i++)
            {
                items[i] = int.Parse(ids[i]);
            }

            return items;
        }

        public string GetCheckedStrItemIDs()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

            return checkedItemID;
        }

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);
            checkedItemID = "(" + checkedItemID + ")";

            String oldFilter = Filter;
            if (!String.IsNullOrEmpty(oldFilter))
                Filter = Filter + " and (" + POSSystem.BLL.Users.ColumnNames.ID + " in " + checkedItemID + ")";
            else
                Filter = POSSystem.BLL.Users.ColumnNames.ID + " in " + checkedItemID;

            Jobs.Rewind();
            do
            {
                string oldPFilter = JobProblems.Filter;
                JobProblems.Filter = RepairJobProblems.ColumnNames.RepairJobID + "=" + Jobs.s_ID;
                do
                {
                    JobProblems.MarkAsDeleted();
                } while (JobProblems.MoveNext());
                Jobs.MarkAsDeleted();
            } while (SourceObject.MoveNext());


            Filter = oldFilter;
        }

        public override void Refresh()
        {
            base.Refresh();
            if (Jobs.RowCount <= 0)
                lblNoRecord.Visible = true;
            else
                lblNoRecord.Visible = false;
            grid.Columns[0].Visible = AllowSelecting;
        }
    }
}